<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="utf-8" %>
<html>
<head>
    <title>首页</title>
</head>
<body>
<%--商品类别Datagrid--%>
<table id="category_list_datagridID" class="easyui-datagrid"
       data-options="url:'${pageContext.request.contextPath}/CategoryServlet?action=findAllCategory',
       fitColumns:true,singleSelect:true,fit:true,border:false,toolbar:'#category_toolbar'">
    <thead>
    <tr>
        <th data-options="field:'cid',width:'50',align:'center'">编号</th>
        <th data-options="field:'cname',width:'50',align:'center'">类别名称</th>
    </tr>
    </thead>
</table>
<%--商品类别Datagrid的工具栏--%>
<div id="category_toolbar">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true"
       onclick="newCategory()">添加</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="updateCategory()">修改</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true"
       onclick="deleteCategory()">删除</a>
</div>
<%--新增和修改类别的Dialog--%>
<div id="category_dialogID" class="easyui-dialog" style="padding:10px 20px"
     closed="true" buttons="#dlg-buttons">
    <form id="category_formID" method="post">
        <table cellpadding="5">
            <caption>商品类别信息</caption>
            <tr>
                <td>类别编号:</td>
                <td><input name="cid" id="cid" readonly class="easyui-textbox"></td>
            </tr>
            <tr>
                <td>类别名称:</td>
                <td><input name="cname" id="cname" class="easyui-textbox" required="true"></td>
            </tr>
        </table>
    </form>
</div>
<%--新增和修改类别的Dialog中的Button--%>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveCategory()">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel"
       onclick="javascript:$('#category_dialogID').dialog('close')">取消</a>
</div>
<script type="text/javascript">
    /*Datagrid相关设置*/
    $("#category_list_datagridID").datagrid({
        pagination: true,
        pageSize: 3,
        pageList: [3, 6, 9],
        nowrap: false,
        striped: true,
        idField: 'id',
        loadMsg: '数据正在努力加载，请稍后...',
        frozenColumns: [[  //固定在表格左侧的栏
            {field: 'ck', checkbox: true}
        ]]
    });
    /*新增商品类别*/
    function newCategory() {
        $('#category_dialogID').dialog('open').dialog('setTitle', '新增商品类别');
        $('#category_formID').form('clear');
        $("#_easyui_textbox_input6").val("此项不用填写,由系统自动生成");
        url = '${pageContext.request.contextPath}/CategoryServlet?action=addCategory';
    }
    /*更新商品类别*/
    function updateCategory() {
        var row = $('#category_list_datagridID').datagrid('getSelected');
        if (row) {
            $('#category_dialogID').dialog('open').dialog('setTitle', '修改商品类别');
            $('#category_formID').form('load', row);
            url = '${pageContext.request.contextPath}/CategoryServlet?action=updateCategory&pid=' + row.cid;
        }

    }
    /*删除商品类别*/
    function deleteCategory() {
        var row = $('#category_list_datagridID').datagrid('getSelected');
        if (row) {
            $.messager.confirm('提示', '您确认要删除<font color="red">【' + row.cname + '】</font>吗?', function (r) {
                if (r) {
                    $.post('${pageContext.request.contextPath}/CategoryServlet?action=deleteCategory', {cid: row.cid}, function (result) {
                        var result = eval('(' + result + ')');
                        if (result.success) {
                            $.messager.show({
                                title: '提示',
                                msg: '删除成功',
                                timeout: 1000,
                                showType: 'slide'
                            });
                            $('#category_list_datagridID').datagrid('reload');
                        } else {
                            $.messager.show({
                                title: '错误',
                                msg: '删除不成功',
                                timeout: 1000,
                                showType: 'slide'
                            });
                        }
                    }, 'text');
                }
            });
        }
    }
    /*保存新增和修改*/
    function saveCategory() {
        $('#category_formID').form('submit', {
            url: url,
            onSubmit: function () {
                return $(this).form('validate');
            },
            success: function (result) {
                var result = eval('(' + result + ')');
                if (result.errorMsg) {
                    $.messager.show({
                        title: '错误',
                        msg: '保持失败',
                        timeout: 1000,
                        showType: 'slide'
                    });
                } else {
                    $.messager.show({
                        title: '提示',
                        msg: "保存成功",
                        timeout: 1000,
                        showType: 'slide'
                    });
                    $('#category_dialogID').dialog('close');
                    $('#category_list_datagridID').datagrid('reload');
                }
            }
        });
    }
</script>
</body>
</html>
